require('../less/home.less')
require('../fonts/iconfont.css')
require('../lib/swiper/swiper.css')
// 引入weui
require('weui')
// 引入weui.js
import weui from 'weui.js';
// 文档加载事件

document.ready(function () {
    //加载导航守卫
    utils.navGuard()
    // 加载taps盒子
    utils.taps(0)
    // 获取本地存储的id
    const userid = localStorage.getItem('userId')
    // 获取今日打卡标签
    const userClock = document.querySelector('#punchIn_data')

    function homeSwiper() {
        const mySwiper = new Swiper('.swiper-container', {
            loop: true, // 循环模式选项
            autoplay: {
                delay: 3000,
                stopOnLastSlide: false,
                disableOnInteraction: false,
            },
            // 如果需要分页器
            pagination: {
                el: '.swiper-pagination',
            },
        })
    };

    homeSwiper();

    // 获取首页数据
    function getHome() {

        axios.get(`${url}/headPageInfo?userId=${userid}`)
            .then(function (res) {
                res = res.data
                if (res.status == 0) {
                    // 先渲染排名
                    const userRank = res.data.rank;
                    const rank = document.querySelector('#rank');
                    rank.textContent = userRank;

                    // 渲染徽章数
                    const userinsigniaNum = res.data.insigniaNum;
                    const insigniaNum = document.querySelector('#insigniaNum');
                    insigniaNum.textContent = userinsigniaNum;

                    // 渲染打卡天数
                    const userpunchIn = res.data.punchIn;
                    const punchIn = document.querySelector('#punchIn');
                    punchIn.textContent = userpunchIn;
                    //渲染是否打卡
                    userClock.textContent = res.data.isPunch === "true" ? '已打卡' : '今日打卡';
                }
            })
            .catch(function (error) {
                console.log(error)
            })
    }
    getHome();

    // 用户打卡
    userClock.addEventListener('click', function () {
        if (userClock.textContent === '今日打卡') {
            axios.get(`${url}/clockIn?userId=${userid}`)
                .then(function (res) {
                    res = res.data
                    if (res.status == 0) getHome()
                })
                .catch(function (error) {
                    console.log(error)
                })
        }else{
            weui.alert('今天已经打过卡了');
        }

    });


    // 课程训练跳转
    const course = document.querySelector('.course');
    course.addEventListener('click', function () {
        location.href = './course.html';
    });
    // 户外跑步跳转
    const run = document.querySelector('.run');
    run.addEventListener('click', function () {
        location.href = './run.html';
    })

})